package com.mxx.common.core;

import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

public class UniquelyList<K, T> extends ArrayList<T> {

    private Map<K, Integer> map;
    private Function<T, K> function;

    public UniquelyList(Function<T, K> function) {
        this.function = function;
        this.map = new HashMap<>();
    }

    public UniquelyList(int initialCapacity, Function<T, K> function) {
        super(initialCapacity);
        this.function = function;
        this.map = new HashMap<>();
    }

    public UniquelyList(Function<T, K> function, @NotNull Collection<? extends T> c) {
        this.function = function;
        this.map = new HashMap<>();
        addAll(c);
    }

    public Integer getIndex(K key) {
        return map.get(key);
    }

    public T getByKey(K key) {
        Integer index = map.get(key);
        if (index != null) {
            return this.get(index);
        }
        return null;
    }

    public Set<K> keySet() {
        return this.map.keySet();
    }

    @Override
    public boolean add(T t) {
        K k = function.apply(t);
        Integer old = map.get(k);
        if (old != null) {
            super.set(old, t);
            return true;
        } else {
            map.put(k, this.size());
            return super.add(t);
        }
    }

    @Override
    public void add(int index, T element) {
        map.put(function.apply(element), index);
        super.add(index, element);
    }

    @Override
    public boolean addAll(int index, Collection<? extends T> c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean addAll(Collection<? extends T> collection) {
        if (collection.isEmpty()) {
            return false;
        }
        for (T c : collection) {
            this.add(c);
        }
        return true;
    }

    @Override
    public T set(int index, T element) {
        map.put(function.apply(element), index);
        return super.set(index, element);
    }

    @Override
    public boolean remove(Object o) {
        throw new UnsupportedOperationException();
    }

    @Override
    public T remove(int index) {
        throw new UnsupportedOperationException();
    }

    @Override
    protected void removeRange(int fromIndex, int toIndex) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean removeIf(Predicate<? super T> filter) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void replaceAll(UnaryOperator<T> operator) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public ListIterator<T> listIterator() {
        throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public ListIterator<T> listIterator(int index) {
        throw new UnsupportedOperationException();
    }

}
